package com.sy.core.website;

import com.sy.core.model.SyOrderInfo;
import com.sy.core.model.SyReceiverMessage;
import com.sy.core.model.SyShoppingCart;
import com.sy.core.service.ShoppingCartService;
import com.sy.core.util.NumUtils;
import com.sy.core.util.RestResult;
import com.sy.core.web.BaseCtl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 购物车，结算页面，支付页面模块
 */
@Controller
@RequestMapping("website/shoppingCart")
public class ShoppingCartCtl extends BaseCtl {

    protected  static String prefix = "/website/";

    /**
     * 购物车新增和修改
     * @param syShoppingCart
     * @return
     */
    @RequestMapping("addAndEdit")
    @ResponseBody
    public RestResult addAndEdit(SyShoppingCart syShoppingCart){
        RestResult rr = null;
        if(!StringUtils.isNotBlank(syShoppingCart.getProductSkuPk())) return new RestResult("500","请选商品择属性");
        if(Integer.parseInt(syShoppingCart.getProductSkuCount())<1) return new RestResult("500","请选择商品数量");
        syShoppingCart.setShoppingCartUserpk(getLoginUser().getPk());
        try {
            String num = shoppingCartService.addAndEdit(syShoppingCart);
            rr = new RestResult("200","操作成功",num);
        } catch (Exception e) {
            rr = new RestResult("500","操作失败");
            logger.error("操作失败");
        }
        return rr;
    }

    /**
     * 购物车列表页面
     */
    @RequestMapping("shopping_list")
    public String list(ModelMap mm,SyShoppingCart syShoppingCart,String shoppingCartPk){
        mm.addAttribute("user",getLoginUser());
        List<Map<String, Object>> syShoppingCartList = shoppingCartService.queryList(syShoppingCart);
        String path="shopping_list_no";
        if(syShoppingCartList!=null&&syShoppingCartList.size()>0){
            mm.addAttribute("shoppingCartList",syShoppingCartList);
            syShoppingCart.setShoppingCartUserpk(getLoginUser().getPk());
            Integer count = shoppingCartService.queryCount(syShoppingCart);
            mm.addAttribute("count",count);
            mm.addAttribute("shoppingCartPk",shoppingCartPk);
            path="shopping_list";
        }
        return prefix+path;
    }

    /**
     * 购物车删除功能
     */
    @RequestMapping("delete")
    @ResponseBody
    public RestResult delete(String pks){
        RestResult rr = null;
        try {
            shoppingCartService.batchDel(pks);
            rr = new RestResult("200","删除成功");
        } catch (Exception e) {
            rr = new RestResult("500","删除成功");
            logger.error("删除失败");
            logger.error(e.toString());
        }
        return rr;
    }

    /**
     * 购物车结算，确认信息页
     * @param pks
     * @param mm
     * @return
     */
    @RequestMapping("shopping_information")
    public String shopping_information(String pks, ModelMap mm, SyReceiverMessage syReceiverMessage){
        mm.addAttribute("user",getLoginUser());
        mm.addAttribute("pks",pks);
        //查询该用户所有的地址
        syReceiverMessage.setReceiverPk(getLoginUser().getPk());
        List<Map<String, Object>> addressList = addressService.queryList(syReceiverMessage);
        mm.addAttribute("addressList",addressList);
        //查询结算的商品
        String[] pkList = pks.split(",");
        List<Map<String,Object>> shoppingCartList = new ArrayList<>();
        String allProductPrice="0";
        String totalPrice=null;
        for (int i = 0; i <pkList.length ; i++) {
            Map<String, Object> shoppingCart = shoppingCartService.queryOne(pkList[i]);
            shoppingCartList.add(shoppingCart);
            totalPrice=((String)shoppingCart.get("total_price")).replaceAll(",","");
            allProductPrice= new BigDecimal(totalPrice).add(new BigDecimal(allProductPrice)).toString();
        }
        mm.addAttribute("allProductPrice", NumUtils.parseMoney(allProductPrice));
        mm.addAttribute("shoppingCartList",shoppingCartList);
        return prefix+"shopping_information";
    }

    /**
     * 购物车结算页面，地址编辑页面回显数据
     */
    @RequestMapping("editAddress")
    @ResponseBody
    public RestResult editAddress(String pk){
        Map<String, Object> address = addressService.queryOne(pk);
        return new RestResult("200","获取地址数据成功",address);
    }

    /**
     * 购物车结算页面，立即支付页面跳转
     */
    @RequestMapping("shopping_payment")
    public String shopping_payment(String productPks,String address,ModelMap mm,SyOrderInfo syOrderInfo){
        mm.addAttribute("user",getLoginUser());
        mm.addAttribute("productPks",productPks);
        mm.addAttribute("address",address);
        mm.addAttribute("syOrderInfo",syOrderInfo);
        return prefix+"shopping_payment";
    }
}
